/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.core;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sigawe.gateway.dao.impl.OutboxDAO;
import net.sigawe.gateway.db.DBConnection;
import net.sigawe.gateway.model.Outbox;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

/**
 *
 * @author Rahman
 */
public class Sender extends Thread implements MessageListener {

    XMPPConnection connection;
    boolean status = true;

    public Sender() {
    }

    public Sender(XMPPConnection connection) {
        this.connection = connection;

    }

    public void sendMessage(String to, String message) throws XMPPException {
        Chat chat = connection.getChatManager().createChat(to, this);
        chat.sendMessage(message);
    }

    @Override
    public void run() {

        System.out.println("Gateway Running............");
        while (status) {
            if (this.connection.isConnected()) {
                try {
                    OutboxDAO odao = new OutboxDAO(DBConnection.getInstance().getCon());
                    if (odao.getAll().size() != 0) {
                        for (Outbox outbox : odao.getAll()) {
                            System.out.println("=======MESSAGE TO SERVER=======");
                            System.out.println("ID Outbox   : " + outbox.getIdOutbox());
                            System.out.println("Receiver    : " + outbox.getReceiver());
                            System.out.println("BODY        : " + outbox.getText());
                            String data = "" + outbox.getReceiver() + "" + outbox.getText() + "";
                            this.sendMessage(outbox.getReceiver(), outbox.getText());
                            odao.delete(outbox.getIdOutbox());
                        }
                        odao.getAll().clear();
                    }
                    try {
                        Thread.sleep(300);
                        System.gc();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (XMPPException ex) {
                    System.out.println("Koneksi Server sigawe terputus");
                // Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    System.out.println("Koneksi dengan database terputus..");
                //   Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else
            {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        }
    }

    public void processMessage(Chat arg0, Message arg1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
